<?php 
namespace app\admin\controller;
use think\Controller;
use think\Request;
use think\Db;
class  Banner extends Admin
{

	public function __construct(){
		parent::initialize();
		// var_dump($this->initialize()) ;

	}
	public function index()
	{
		$list = Db::name('tb_banner')->select();
		return $this->fetch('banner/banner',[
					'list'=>$list,

			]);
	}

	public function addbanner()
	{
		return $this->fetch('banner/addbanner');
	}

	public function add(Request $request)
	{
		$file = request()->file('image');
		$data = Request::instance()->post();
		var_dump($file,$data);
		if ($file){
		    $info = $file->validate(['size'=>156780000,'ext'=>'jpg,png'])->move(ROOT_PATH . 'public' . DS . 'uploads/banner');
			$img = $info->getSaveName();
			$imgs = str_replace('\\', '/', $img);
		    $result=Db::table('tb_banner')
                    ->data(['img'=>$imgs,'status'=>$data['status'],'url'=>$data['url']])
                    ->insert();
		}else{
			$this->error('图片传输错误');
		}


		if ($result) {
			$this->success('添加成功',url('index'));
		}else{
			$this->error('添加失败');
		}
	}


	public function del(Request $request)
	{
		$id = $_POST['id'];
		$list = Db::name('tb_banner')->where('id',$id)->delete();
		echo json_encode($list);
	}


	public function status(Request $request)
	{
		$id = $_POST['id'];
		$res = Db::name('tb_banner')->where('id',$id)->value('status');
		if ($res==1) {
			$list = Db::name('tb_banner')->where('id',$id)->update(['status'=>2]);
			return 2;			
		}else{
			$list = Db::name('tb_banner')->where('id',$id)->update(['status'=>1]);	
			return 1;					
		}
	}
}

